#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;


vector<int> intersection(vector<int>& nums1, vector<int>& nums2)
{
    sort(nums1.begin(), nums1.end());
    sort(nums2.begin(), nums2.end());
    vector<int> ans;

    int left1 = 0; int left2 = 0;
    int end1 = nums1.size() - 1;
    int end2 = nums2.size() - 1;

    int key = -1;
    while (left1 <= end1 && left2 <= end2)
    {

        while (left1 <= end1 && nums1[left1] == key) left1++;
        while (left2 <= end2 && nums2[left2] == key) left2++;

        if (left1 > end1 || left2 > end2) break;

        if (left2 == 7)
        {
            int ajiofhs = 0;
        }

        if (nums1[left1] == nums2[left2])
        {
            key = nums1[left1];
            ans.push_back(key);
            left1++; left2++;
        }
        else if (nums1[left1] < nums2[left2])
        {
            int k1 = nums1[left1];
            while (left1 <= end1 && nums1[left1] == k1)
            {
                left1++;
            }
        }
        else if (nums1[left1] > nums2[left2])
        {
            int k2 = nums2[left2];
            while (left2 <= end2 && nums2[left2] == k2)
            {
                left2++;
            }
        }
    }

    return ans;
}

int main()
{
    vector<int> a = { 1,2,2,3,4,634,54,653,7,7 };
    vector<int> b = { 1,22,54,54,36,7,3,2,3,653 };
    vector<int>c = intersection(a, b);
    for (int num : c)
    {
        cout << num << " ";
    }

    int r = 0;
    return 0;
}